package com.oss.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author xueqiang.mi
 * @since 2010-8-2
 */
@Controller
public class TopicController extends BaseController {

	@RequestMapping("/topic.do")
	public String topic(
			Model model,
			@RequestParam(value = "id") String id,
			@RequestParam(value = "pageIndex", required = false) Long pageIndex,
			@RequestParam(value = "pageSize", required = false) Long pageSize) {
		if (pageIndex == null || pageSize == null) {
			return "redirect:/topic.do?id=" + id + "&pageIndex=1&pageSize="
					+ PRJ_MEDIUM_SIZE;
		}
		pageIndex = pageIndex > 0 ? pageIndex : 1;
		pageSize = pageSize > 50 ? 50 : (pageSize > 0 ? pageSize
				: BaseController.PRJ_MEDIUM_SIZE);
		model.addAttribute("topic", topicService.get(id));
		model.addAttribute("links", lnkService.getLnksByTopic(id, pageIndex,
				pageSize));
		return "topic";
	}

	@RequestMapping("/topics.do")
	public String topics(
			Model model,
			@RequestParam(value = "pageIndex", required = false) Long pageIndex,
			@RequestParam(value = "pageSize", required = false) Long pageSize) {
		if (pageIndex == null || pageSize == null) {
			return "redirect:/topics.do?pageIndex=1&pageSize=25";
		}
		pageIndex = pageIndex > 0 ? pageIndex : 1;
		pageSize = pageSize > 50 ? 50 : (pageSize > 0 ? pageSize : 10);
		model.addAttribute("topics", topicService
				.getTopics(pageIndex, pageSize));
		return "topics";
	}
}
